package com.zhuzi.guicollect.utils;

import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.HierarchicalINIConfiguration;
import org.springframework.core.io.ClassPathResource;

import java.io.File;
import java.io.IOException;

/**
 * @author MI
 * @ClassName IniUtils.java
 * @createTime 2021年10月31日 18:38:00
 */
public class IniUtils {

    private static HierarchicalINIConfiguration iniConfiguration;

    static {
        try {
            File file = new ClassPathResource("my.ini").getFile();
            iniConfiguration = new HierarchicalINIConfiguration(file);
        } catch (IOException | ConfigurationException e) {
            e.printStackTrace();
        }
    }

    public static String read(String group, String key) {
        //更新
        return iniConfiguration.getSection(group).getString(key);
    }

    public static void update(String group, String key, String value) {
        try {
            iniConfiguration.getSection(group).setProperty(key, value);
            iniConfiguration.save();
        } catch (ConfigurationException e) {
            e.printStackTrace();
        }
    }
}
